Skip to content

exec-builtin: flag non-call references in preview mode - #28051

Open
CAOShurong wants to merge 3 commits into
astral-sh:mainfrom
CAOShurong:fix/exec-builtin-references
Open

exec-builtin: flag non-call references in preview mode#28051
CAOShurong wants to merge 3 commits into
astral-sh:mainfrom
CAOShurong:fix/exec-builtin-references

Conversation

@CAOShurong

Copy link
Copy Markdown

Summary

exec-builtin (S102) currently flags only direct calls to the builtin exec, while the parallel rule suspicious-eval-usage (S307) also flags non-call references like map(eval, ...) or foo = eval in preview mode (behavior added in #15522/#15523). This makes the two rules inconsistent:

exec("hi")            # S102 fires
list(map(exec, []))   # nothing -- but map(eval, []) gets S307 in preview

This extends the existing preview-mode reference detection (suspicious_function_reference) to cover exec, so both rules treat references consistently. The reference diagnostics are gated behind the same preview flag as S307's, so stable-mode output is unchanged.

Calls are still reported exactly once via the existing call-site check (exec_used); the new arm only matches non-call references to avoid duplicate diagnostics.

Fixes #28011

Test Plan

  • New fixture cases in flake8_bandit/S102.py: list(map(exec, ["hi"])) and foo = exec (both flagged in preview), plus a shadowed local exec (not flagged).
  • Added a preview_rules test case for ExecBuiltin/S102.py; snapshot shows exactly the two added reference diagnostics vs. stable mode.
  • cargo test -p ruff_linter --release --lib green (2797 passed); cargo fmt --check clean.

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 25, 2026 21:37
@astral-sh-bot

astral-sh-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good overall. I think we can consolidate the stable and preview checks, though.

Comment on lines +1165 to +1169
// Exec
// Calls to `exec` are reported by [`exec_used`]; here we only flag
// non-call references (see `suspicious_function_reference`) to avoid
// duplicate diagnostics.
["" | "builtins", "exec"] if arguments.is_none() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just remove the check in exec_used? It just seems confusing to split the check if both could be handled here, which I think is the case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make this change, which I still think we should, we need to use func.range() here to preserve the range from exec_used:

/// S102
pub(crate) fn exec_used(checker: &Checker, func: &Expr) {
if checker.semantic().match_builtin_expr(func, "exec") {
checker.report_diagnostic(ExecBuiltin, func.range());
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update this list to include S102.

Suggested change
/// S102, S301, S302, S303, S304, S305, S306, S307, S308, S310, S311, S312, S313, S314, S315, S316, S317, S318, S319, S320, S321, S323

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Aug 28, 2026
CAOShurong and others added 2 commits August 30, 2026 03:35
`exec-builtin` (S102) only flagged direct calls to the builtin
`exec`, while `suspicious-eval-usage` (S307) also flags references
such as `map(eval, ...)` or `foo = eval` in preview mode. Extend the
preview-mode reference detection in `suspicious_function` to cover
`exec` so both rules behave consistently.

Calls remain reported once via the existing call-site check;
references are preview-gated behind the same
`suspicious_function_reference` machinery used by S307.

Fixes astral-sh#28011
@CAOShurong
CAOShurong force-pushed the fix/exec-builtin-references branch from c0c7351 to a221927 Compare August 29, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exec-builtin should also warn on references to the exec function

2 participants